package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.Advertising;

/***
 * @desc   广告实现类
 * @author lph
 *
 */
@Service
public class AdvertisingService extends BaseDao<Advertising>{
	/**
	 * 按条件查询
	 * @param TeachAdvice
	 * @return
	 */
	public List<Advertising> query(Advertising advertising, String... date) {
		if(null == advertising) return super.findAll();
		if(advertising.getiDisplayLength() > 0){
		  return super.find(createFilter(advertising, date),advertising.getiDisplayStart(),advertising.getiDisplayLength());
		}
		return super.find(createFilter(advertising, date));
	}

	public Long count(Advertising advertising, String... date) {
		return super.count(createFilter(advertising, date));
	}
	
	/**
	 * 拼装条件
	 * @param evaluationProject
	 * @param date
	 * @return
	 */
	private DetachedCriteria createFilter(Advertising advertising, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  
	
	  
	
	  if(!StringUtil.isNullOrEmpty(advertising.getName())){
		  dc.add(Restrictions.like("name", advertising.getName().trim(), MatchMode.ANYWHERE));
	  }
	  if(!StringUtil.isNullOrEmpty(advertising.getLiveId())){
		  dc.add(Restrictions.eq("liveId", advertising.getLiveId()));
	  }
	  if(!StringUtil.isNullOrEmpty(advertising.getLiveName())){
		  dc.add(Restrictions.like("liveName", advertising.getLiveName().trim(), MatchMode.ANYWHERE));
	  }
	  if(!StringUtil.isNullOrEmpty(advertising.getAdvertisingType())&&advertising.getAdvertisingType().equals("2")){
		  dc.add(Restrictions.like("advertisingType", advertising.getAdvertisingType().trim(), MatchMode.ANYWHERE));
	  }else{
		  dc.add(Restrictions.or(Restrictions.eq("advertisingType", "1"),Restrictions.eq("advertisingType", "3")));//查询合作广告1和合作广告2

	  }
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
		    dc.add(Restrictions.between("insertTime", date[0],date[1]));
		  }

	  //dc.addOrder(Order.desc("insertTime"));
		return dc;
	}
}
